package com.hxzy.servlet.UmsAdminLoginLog;

import com.hxzy.entity.UmsAdminLoginLog;
import com.hxzy.service.UmsAdminLoginLogService;
import com.hxzy.service.impl.UmsAdminLoginLogServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/UmsAdminLoginLog/update")
public class update extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        //接收网页地址传入的参数
        String value = req.getParameter("id");

        UmsAdminLoginLogService service=new UmsAdminLoginLogServiceImpl();
        UmsAdminLoginLog umsAdminLoginLog = service.selectByPrimaryKey(Long.parseLong(value));

        req.setAttribute("id",umsAdminLoginLog);
        req.getRequestDispatcher("/WEB-INF/UmsAdminLoginLog/update.jsp").forward(req,res);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=UTF-8");

        UmsAdminLoginLog loginLog=new UmsAdminLoginLog();

        Long adminId=Long.getLong(req.getParameter("adminId"));
        loginLog.setAdminId(adminId);

        SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-DD");
        try {
            Date createTime=sdf.parse(req.getParameter("createTime"));
            loginLog.setCreateTime(createTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String ip=req.getParameter("ip");
        loginLog.setIp(ip);

        String address=req.getParameter("address");
        loginLog.setAddress(address);

        Long id=Long.parseLong(req.getParameter("id"));
        loginLog.setId(id);

        UmsAdminLoginLogService service=new UmsAdminLoginLogServiceImpl();
        boolean insert = service.insert(loginLog);

        PrintWriter out=res.getWriter();
        out.println( insert? "修改成功": "修改失败");
        //输出刷新
        out.flush();
        out.close();

    }
}
